from io import StringIO

import pandas as pd
import numpy as np
#
# # pd1 = pd.Series()
# # print(pd1, type(pd1))
#
# # 使用numpy中的矩阵创建
# array1 = np.array([11, 22, 33, 44])
# print(array1, type(array1))
# # 重新给索引值
# s1 = pd.Series(data=array1, index=[1001, 1002, 1003, 1004])
# print(s1, type(s1))
# print(s1[1002])
# # print(s1[1]) # 报错
#
# print("----------------------------------------------")
# # 将python中字典数据转成Series对象
# dict1 = {'1001': '小虎', '1002': '张三', '1003': '李四', '1902': '王五'}
# # 取出想要的索引
# s2 = pd.Series(data=dict1, index=['1001', '1003'])
# print(s2, type(s2))
# print(s2['1001'])  # 使用本身字典中的键索引
# print(s2[0])  # 也可以使用默认索引获取
#
# print("----------------------------------------------")
# list1 = [1001, 1002, 1003, 1004]
# s3 = pd.Series(data='shujia', index=list1)
# print(s3, type(s3))
#
# print(s3[1001])  # 使用本身自定义的索引
# # print(s3[0])
#
# print("----------------------------------------------")
# list1 = [1001, 1002, 1003, 1004, 1005]
# s4 = pd.Series([11, 22, 33, 44, 55], index=list1)
# print(s4, type(s4))
# # print(s4[1004]) # 使用本身自定义的索引
# # print(s4[3])
# print("----------------------------------------------")
# s = pd.Series([6, 7, 8, 9, 10], index=['a', 'b', 'c', 'd', 'e'])
# print(s['d'])
# print(s[['b', 'c', 'd']])
# print("----------------------------------------------")
#
# res1 = np.random.randn(5)
# print(res1)
#
# s = pd.Series(np.random.randn(5))
# print(s)
# print("所有行索引标签:")
# print(s.axes)
# print("所有行索引标签:")
# print(s.values)
# print("----------------------------------------------")
# s = pd.Series([1, 2, 5, 8], index=['a', 'b', 'c', 'd'])
# print(s.index)
# # 隐式索引
# s1 = pd.Series([1, 2, 5, 8])
# print(s1.index)
# print("----------------------------------------------")
# s = pd.Series(np.random.randn(8))
# print(s)
# # 返回前三行数据
# print(s.head(5))
# print(s.tail(2), type(s.tail(2)))
# print(list(s.tail(2)))
# print("----------------------------------------------")
# dict1 = {'1001': '小虎', '1002': '张三', '1003': '李四', '1902': '王五'}
# # 取出想要的索引
# s2 = pd.Series(data=dict1, index=['1001', '1003','1009'])
#
# print(pd.notnull(s2))
# print("----------------------------------------------")
# df1 = pd.DataFrame()
# print(df1, type(df1))
# print("----------------------------------------------")
# list1 = ['zhangsan','lisi','wangwu','zhaoliu']
# df2 = pd.DataFrame(list1,index=[1001,1002,1003,1004],columns=['姓名'])
# print(df2)
# print("----------------------------------------------")
# list1 = [['zhangsan',18,'男','33期'],['lisi',15,'男','33期'],['wangwu',13,'女','33期'],['zhaoliu',19,'男','33期']]
# df2 = pd.DataFrame(list1,index=[1001,1002,1003,1004],columns=['姓名','年龄','性别','班级'], dtype='float64')
# print(df2)
# print("----------------------------------------------")
# # 字典中的键，作为DataFrame中的列名存在，后面值作为一列存在
# '''
# dict1 = {
#     'name':['zhangsan','lisi','wangwu','zhaoliu'],
#     'age':[18,15,15,11]',
#     'gender':['女','男','女','男'],
#     'clazz':['33期''33期','33期','33期']
# }
# '''
# dict1 = {
#     'name':['zhangsan','lisi','wangwu','zhaoliu'],
#     'age':[18,15,15,11],
#     'gender':['女','男','女','男'],
#     'clazz':['33期','33期','33期','33期']
# }
#
# df3 = pd.DataFrame(dict1, index=[1001,1002,1003,1004])
# print(df3)
# print("----------------------------------------------")
# data = [{'a': 1, 'b': 2, 'd':5},{'a': 5, 'b': 10, 'c': 20}]
# df = pd.DataFrame(data)
# print(df)
# print("----------------------------------------------")
# data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
# df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b', 'b1'])
# print(df1)
# print("----------------------------------------------")
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
# df = pd.DataFrame(d)
# print(df)
# print("-------")
# print(df['one'])
# print("----------------------------------------------")
# d = {'Name' : pd.Series(['xiaohu1', 'xiaohu2', 'xiaohu3','xiaohu4'], index=['a', 'b', 'c','d']),
#    'math' : pd.Series([89, 92, 91], index=['a', 'b', 'c'])}
# df = pd.DataFrame(d)
# print(df)
# print("-------")
# df['english']=pd.Series([67,78,79,99],index=['a','c','b','d'])
# print(df)
#
# df['sum_score'] = df['math'] + df['english']
# print(df)
# print("-------")
# info=[['xiaohu',18],['xiaoge',19],['fengfeng',17]]
# df=pd.DataFrame(info,columns=['name','age'])
# print(df)
# #注意是column参数
# #数值1代表插入到columns列表的索引位置
# df.insert(1,column='score',value=[91,90,75])
# print(df)
# print("-------")
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
#    'three' : pd.Series([10,20,30], index=['a','b','c'])}
# df = pd.DataFrame(d)
# print(df)
#
# # del df['one']
# # print(df)
# #
# # df.pop('three')
# # print(df)
#
# s1 = df.loc['b']
# print(s1, type(s1))
# print("-------")
# d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
#    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
#
# df = pd.DataFrame(d)
# print(df)
# print (df.iloc[2])
# print(df[2:4])
# print("-------")
# df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
# df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','c'])
# #在行末追加新数据行
# df = df.append(df2)
# print(df)
# print("--------------------")
# d = {'Name':pd.Series(['python编程大全','hadoop基础',"hive进阶",'spark基础','flink进阶','mysql从入门到成神','java之路']),
#    'years':pd.Series([5,6,15,28,3,19,23]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
# #构建DataFrame
# df = pd.DataFrame(d)
# #输出series
# print(df)
# print(df.T)
# print(df.axes)
# print(df.dtypes)
# print(df.ndim)
# print(df.shape)
# print(df.size)
# print(df.values)
# print("-------------------------")
# info= pd.DataFrame({'a_data': [40, 28, 39, 32, 18],
# 'b_data': [20, 37, 41, 35, 45],
# 'c_data': [22, 17, 11, 25, 15]})
# print(info)
#
# res1 = info.shift(periods=-2, axis=1)
# print(res1.fillna('缺省值'))
# print("----------------------------------------------")
# d = {'Name' : pd.Series(['xiaohu1', 'xiaohu2', 'xiaohu3','xiaohu4'], index=['a', 'b', 'c','d']),
#    'math' : pd.Series([89, 92, 91], index=['a', 'b', 'c'])}
# df = pd.DataFrame(d)
# print(df)
# print("-------")
# df['english']=pd.Series([67,78,79,99],index=['a','c','b','d'])
# print(df)
# print("-------")
# df2 = df.fillna(0) # 将空值进行填充处理
# df2['sum_score'] = df2['math'] + df2['english']
# print(df2)
# print("--------------")
# #创建字典型series结构
# d = {'Name':pd.Series(['小虎','小杰','小龙','小明','小赵','小李','小陈',
#    '老李','老王','小唐','老覃','小阳']),
#    'Age':pd.Series([25,26,25,23,18,29,18,14,19,20,24,21]),
#    'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
# }
# df = pd.DataFrame(d)
# print(df)
# print("---------")
# print(df.describe())
# print("-----------------")
# df1 = pd.read_csv('data/demo1.csv')
# print(df1, type(df1))
# data = ('col1,col2,col3\n'
#         'a,b,1\n'
#         'a,b,2\n'
#         'c,d,3')
#
# df = pd.read_csv(StringIO(data), dtype=object)
# print(df)
# print("-----------------")
# df1 = pd.read_csv('data/demo11.csv', encoding='gbk')
# print(df1, type(df1))

print("----------------------------------------------")
d = {'Name' : pd.Series(['xiaohu1', 'xiaohu2', 'xiaohu3','xiaohu4'], index=['a', 'b', 'c','d']),
   'math' : pd.Series([89, 92, 91], index=['a', 'b', 'c'])}
df = pd.DataFrame(d)
print(df)
print("-------")
df['english']=pd.Series([67,78,79,99],index=['a','c','b','d'])
print(df)
print("-------")
df2 = df.fillna(0) # 将空值进行填充处理
df2['sum_score'] = df2['math'] + df2['english']
print(df2)

df2.to_csv('data/out2.csv',encoding='UTF-8',index=False, sep='|')


